package com.yanyu.space.file.controller;

import com.yanyu.space.common.core.bean.web.R;
import com.yanyu.space.common.core.exception.ServiceException;
import com.yanyu.space.file.bean.bo.*;
import com.yanyu.space.file.bean.enums.FileRCode;
import com.yanyu.space.file.service.IFileInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能: 文件 前端控制器
 *
 * @author yanyu
 * @date 2022-11-15
 */
@RestController
@RequestMapping("/file/fileInfo")
@Api(tags = "文件")
public class FileInfoController {

    @Autowired
    private IFileInfoService fileInfoService;

    @PostMapping(value = "/upload")
    public R<List<FileInfoBo>> directUpload(FileDirectSearchBo bo) {
        return R.ok(fileInfoService.directUpload(bo));
    }

    @PostMapping(value = "/sharding")
    public R<FileShardingInfoBo> shardingUpload(FileShardingSearchBo bo) {
        FileShardingInfoBo fileShardingInfoBo = fileInfoService.shardingUpload(bo);
        return R.ok(fileShardingInfoBo);
    }

    @PostMapping(value = "/merge")
    public R<FileInfoBo> shardingMergeUpload(FileShardingMergeSearchBo bo) {
        FileInfoBo fileInfoBo = fileInfoService.shardingMergeUpload(bo);
        if (fileInfoBo == null) {
            throw new ServiceException(FileRCode.FILE_MERGE_FAIL);
        }
        return R.ok(fileInfoBo);
    }
}
